import Department from './Department';
import moment, { Moment } from 'moment';
import { addDepartment } from './store';
import { UploadFile } from 'antd/lib/upload/interface';
import { parseFileToSheetJson } from './fileToModelUtil';
import { REG_TIME_MOMENT } from './dateUtil';

export function setDefaultDepartment(clockMoment: [Moment, Moment], lateAndEarlyMoment: [Moment, Moment]) {
    const defaultDepartment = new Department({
        id: 'default',
        name: '默认',
        clockInMoment: clockMoment[0],
        clockOutMoment: clockMoment[1],
        beLateMoment: lateAndEarlyMoment[0],
        absenteeismLateMoment: lateAndEarlyMoment[0],
        absenteeismEarlyMoment: lateAndEarlyMoment[1]
    });

    addDepartment(defaultDepartment);
}

export function parseDepartmentByFileList(departmentFileList: UploadFile[]): Promise<void> {
    return new Promise((resolve, reject) => {
        const promiseList: Promise<void>[] = [];
        departmentFileList.forEach(departmentFile => {
            if (departmentFile.originFileObj !== undefined) {
                promiseList.push(parseDepartmentByFile(departmentFile));
            }
        });
        Promise.all(promiseList).then(() => {
            resolve();
        }, reject);
    });
}

export function parseDepartmentByFile(departmentFile: UploadFile): Promise<void> {
    return new Promise((resolve, reject) => {
        if (departmentFile.originFileObj === undefined) {
            reject(`《${departmentFile.name}》是无效的考勤记录文件，请严格按照格式要求上传数据和格式正确的文件。`);
        } else {
            parseFileToSheetJson(departmentFile, '部门信息').then(departmentJson => {
                if (departmentJson.length > 1) {
                    for (let i = 1, limit = departmentJson.length; i < limit; i++) {
                        const departmentInfo = departmentJson[i];
                        let [id, name, clockInMomentStr, absenteeismLateMomentStr, absenteeismEarlyMomentStr, clockOutMomentStr] = departmentInfo;
                        id = String(id);
                        name = String(name);
                        if (id.length > 0 && name.length > 0 && REG_TIME_MOMENT.test(clockInMomentStr) && REG_TIME_MOMENT.test(absenteeismLateMomentStr) && REG_TIME_MOMENT.test(absenteeismEarlyMomentStr) && REG_TIME_MOMENT.test(clockOutMomentStr)) {
                            const momentList: Moment[] = [moment(clockInMomentStr, 'HH:mm:ss'), moment(absenteeismLateMomentStr, 'HH:mm:ss'), moment(absenteeismEarlyMomentStr, 'HH:mm:ss'), moment(clockOutMomentStr, 'HH:mm:ss')];
                            momentList.sort((a, b) => {
                                return a.isBefore(b) ? -1 : 1;
                            });
                            const [clockInMoment, absenteeismLateMoment, absenteeismEarlyMoment, clockOutMoment] = momentList;
                            let beLateMoment = clockInMoment.clone().add(10, 'minutes');
                            if (beLateMoment.isAfter(absenteeismLateMoment)) {
                                beLateMoment = absenteeismLateMoment.clone();
                            }
                            addDepartment(new Department({
                                id,
                                name,
                                clockInMoment,
                                beLateMoment,
                                absenteeismLateMoment,
                                absenteeismEarlyMoment,
                                clockOutMoment
                            }));
                        }
                    }
                }
                resolve();
            }, reject);
        }
    });
}